package com.hcinfos.gsoo.ui;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.hcinfos.gsoo.GsooApplication;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Common;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.https.HttpClientUtil;
import com.hcinfos.gsoo.util.IntentUtil;
import com.hcinfos.gsoo.util.LogUtil;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * gsoo号注册
 * 
 * @author xi
 * 
 */
public class GsooAccountRegActivity extends Activity implements OnClickListener {

	private TextView agreeTv;
	private TextView reg_tv;
	private ImageButton left_ib;
	private TextView center_tv;

	private Button reg_submit_button;
	private EditText reg_password;
	private EditText reg_conform_pwd;
	private EditText reg_username;
	private TextView msg_tv;
	private ProgressBar bar;

	private Animation shake;
	private Context context;
	private Dialog dialog;

	private final int REG_SUUCESS = 0x1120;
	private final int REG_FAIL = REG_SUUCESS + 1;
	private final int SUCCESS_DIALOG_DISMISS = REG_SUUCESS + 2;
	private final int FAIL_DIALOG_DISMISS = REG_SUUCESS + 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gsoo_reg);
		context = this;
		shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		initRes();
	}

	private void initRes() {
		left_ib = (ImageButton) findViewById(R.id.left_ib);
		reg_tv = (TextView) findViewById(R.id.reg_tv);
		left_ib.setOnClickListener(this);
		reg_tv.setOnClickListener(this);
		reg_tv.setVisibility(View.GONE);
		center_tv = (TextView) findViewById(R.id.center_tv);
		center_tv.setText("Gsoo号注册");

		reg_conform_pwd = (EditText) findViewById(R.id.reg_conform_pwd);
		reg_password = (EditText) findViewById(R.id.reg_password);
		reg_submit_button = (Button) findViewById(R.id.reg_submit_button);
		reg_submit_button.setOnClickListener(this);
		reg_username = (EditText) findViewById(R.id.reg_username);

		LayoutInflater inflater = getLayoutInflater();
		RelativeLayout dialog_rl = (RelativeLayout) inflater.inflate(
				R.layout.progress_wait_view, null);
		msg_tv = (TextView) dialog_rl.findViewById(R.id.RequestLoadingText);
		bar = (ProgressBar) dialog_rl.findViewById(R.id.requestLoadingProgress);
		msg_tv.setText("注册中..");

		dialog = new Dialog(this, R.style.QuitDialog);
		dialog.setContentView(dialog_rl);
		Window dialogWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = (int) (280 * Common.PROPORTION); // 宽度
		lp.height = (int) (180 * Common.PROPORTION); // 高度
		dialogWindow.setAttributes(lp);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_ib:
			finish();
			break;
		case R.id.reg_submit_button:
			if (regCheck()) {
				dialog.show();
				reg();
			}
			break;

		}
	}

	private void reg() {
		final String username = reg_username.getText().toString();
		final String password = reg_password.getText().toString();
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					String result = HttpClientUtil.getFromWebByHttpClient(
							context, Urls.USER_REG_GSOO + "&user.username="
									+ username + "&user.password=" + password);
					JSONObject obj = new JSONObject(result);
					int state = -1;
					String number = null;
					if (!obj.isNull("state")) {
						state = obj.getInt("state");
					}
					if (!obj.isNull("data")) {
						number = obj.getJSONObject("data")
								.getString("ggnumber");
					}
					if (state == 200) {
						Message msg = handler.obtainMessage();
						msg.what = REG_SUUCESS;
						msg.obj = number;
						handler.sendMessage(msg);
					} else {
						Message msg = handler.obtainMessage();
						msg.what = REG_FAIL;
						handler.sendMessage(msg);
					}
					LogUtil.d("wxf", "result:" + result);
				} catch (Exception e) {
					Message msg = handler.obtainMessage();
					msg.what = REG_FAIL;
					handler.sendMessage(msg);
					LogUtil.e("wxf", "gsooReg=" + e.toString());
				}

			}
		}).start();
	}

	String gNumber = null;
	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			bar.setVisibility(View.GONE);
			switch (msg.what) {
			case REG_SUUCESS:
				if (msg.obj != null) {
					gNumber = (String) msg.obj;
					msg_tv.setText("Gsoo号:" + gNumber);
				} else {
					msg_tv.setText("抱歉,注册失败");
				}
				handler.sendEmptyMessageDelayed(SUCCESS_DIALOG_DISMISS, 3000);
				break;
			case REG_FAIL:
				handler.sendEmptyMessageDelayed(FAIL_DIALOG_DISMISS, 3000);
				msg_tv.setText("抱歉,注册失败");
				break;
			case SUCCESS_DIALOG_DISMISS:
				if (dialog != null)
					dialog.dismiss();
				GsooApplication.ggNumber = gNumber;
				GsooApplication.password = reg_password.getText().toString();
				IntentUtil.start_activity(GsooAccountRegActivity.this,
						LoginActivity.class, new BasicNameValuePair("email",
								gNumber), new BasicNameValuePair("password",
								reg_password.getText().toString()));
				finish();
				break;
			case FAIL_DIALOG_DISMISS:
				if (dialog != null)
					dialog.dismiss();
				break;
			}
		};
	};

	private boolean regCheck() {
		String username = reg_username.getText().toString();
		String password = reg_password.getText().toString();
		String repassword = reg_conform_pwd.getText().toString();
		if (username.length() == 0) {
			Toast.makeText(this, "用户名不能为空", 500).show();
			reg_username.startAnimation(shake);
			return false;
		}
		if (password.length() < 6) {
			Toast.makeText(this, "密码长度必须为5位以上", 500).show();
			reg_password.startAnimation(shake);
			return false;
		}
		if (repassword.length() == 0 || !repassword.equals(password)) {
			Toast.makeText(this, "2次输入的密码不一致", 500).show();
			reg_conform_pwd.startAnimation(shake);
			return false;
		}
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
